using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(Rigidbody2D))]
public class BezierCurveMotionEffect : LogicalEffectBase
{
    BezierCurveMotionEffectData data;
    private bool arrived = false;
    private Rigidbody2D rb2D;
    public override void Init(GameObject originalGameObject, object[] args = null)
    {
        base.Init(originalGameObject, args);
        data = effectData as BezierCurveMotionEffectData;
        rb2D = GetComponent<Rigidbody2D>();
        C_FollowTarget followTarget = originalGameObject.GetComponent<C_FollowTarget>();
        data.targetPos = followTarget ? followTarget.GetTargetPoint() : Vector2.zero;
    }
    public override void OnApply()
    {

    }
    public override void OnUpdate()
    {
        Vector2 direction = (data.targetPos - transform.position).normalized;
        if (!arrived)
        {
            transform.right = Vector3.Slerp(transform.right, direction, data.lerp / Vector2.Distance(transform.position, data.targetPos));
            rb2D.velocity = transform.right * data.speed;
        }
        if (Vector2.Distance(transform.position, data.targetPos) < 0.5f && !arrived)
        {
            arrived = true;
        }
    }
    public override bool GetLifeState()
    {
        return true;
    }
}
